package com.mmf.ancientcostume.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mmf.ancientcostume.MyApplication;
import com.mmf.ancientcostume.R;
import com.mmf.ancientcostume.adapter.home.ReleaseInfoImageAdapter;
import com.mmf.ancientcostume.base.activity.BaseTitleActivity;
import com.mmf.ancientcostume.common.utils.DipUtil;
import com.mmf.ancientcostume.model.StaticData;
import com.mmf.ancientcostume.other.zhy.imageloader.SelPhotoActivity;
import com.mmf.ancientcostume.presenter.imp.goods.AddCommentPresenterImp;
import com.mmf.ancientcostume.view.BaseView;
import com.mmf.ancientcostume.widget.AppPublicMethod;
import com.mmf.ancientcostume.widget.ReleaseAdapterInterface;
import com.mmf.ancientcostume.widget.SpaceItemDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by MMF on 2017-10-17.
 */

public class AddCommentActivity extends BaseTitleActivity<AddCommentPresenterImp, AddCommentActivity> implements BaseView<String> {
    @BindView(R.id.edt_comment)
    EditText edtComment;
    @BindView(R.id.rv_comment_img)
    RecyclerView rvCommentImg;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.rb_good)
    RadioButton rbGood;
    @BindView(R.id.rb_medium_review)
    RadioButton rbMediumReview;
    @BindView(R.id.rb_bad_review)
    RadioButton rbBadReview;
    @BindView(R.id.rg_comment)
    RadioGroup rgComment;
    private ReleaseInfoImageAdapter adapter;
    private List<String> imgUrls = new ArrayList<>();
    private int level = 0;      //0：好评 1：中评 2：差评
    private int goodsId;        //商品id

    @Override
    public int getLayout() {
        return R.layout.activity_add_comment;
    }

    @Override
    public void init() {
        goodsId = getIntent().getIntExtra("id", 71);
        //设置进入界面不执行getData的方法
        isGetData = false;
        //将RecyclerView设置成gridview的样式 每行显示4个
        GridLayoutManager mgr = new GridLayoutManager(this, 4);
        int space = DipUtil.dip2px(this, 4);
        //设置显示的适配器
        adapter = new ReleaseInfoImageAdapter(this, 4);
        adapter.setItems(imgUrls);
        rvCommentImg.setLayoutManager(mgr);
        //设置RecyclerView每个item的间距
        rvCommentImg.addItemDecoration(new SpaceItemDecoration(space, 4));
        rvCommentImg.setAdapter(adapter);
        adapter.setReleaseAdapterInterface(new ReleaseAdapterInterface() {
            @Override
            public void jumpSelImage() {
                jumpSelPhoto(StaticData.RELEASE_SEL_IMAGE);
            }

            @Override
            public void jump(int position, List<String> itemList) {
                Intent intent = new Intent(AddCommentActivity.this, ImagePreviewActivity.class);
                intent.putExtra("type", "1");
                intent.putExtra("selPosition", position);
                intent.putStringArrayListExtra("imgUrls", (ArrayList<String>) imgUrls);
                intent.putStringArrayListExtra("imgPath", (ArrayList<String>) itemList);
                startActivityForResult(intent, 1);
            }
        });

        rgComment.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rb_good) {
                    level = 0;
                }
                if (i == R.id.rb_medium_review) {
                    level = 1;
                }
                if (i == R.id.rb_bad_review) {
                    level = 2;
                }
            }
        });
    }

    private void jumpSelPhoto(int requestCode) {
        Intent intent = new Intent(this, SelPhotoActivity.class);
        intent.putExtra("maxNum", 9 - imgUrls.size());
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected AddCommentPresenterImp getPresenter() {
        return new AddCommentPresenterImp();
    }

    @Override
    protected void getData() {
        if (MyApplication.getUserId() != -1) {
            presenter.insertComment(getParams(), getImgRequestBody());
        }
    }

    private Map<String, Object> getParams() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("level", level);
        map.put("content", edtComment.getText().toString());
        map.put("goodsId", goodsId);
        map.put("userId", MyApplication.getUserId());
        return map;
    }

    /**
     * 图片信息
     *
     * @return
     */
    private Map<String, RequestBody> getImgRequestBody() {
        Map<String, RequestBody> bodyMap = new HashMap<String, RequestBody>();
        if (imgUrls.size() > 0) {
            for (int i = 0; i < imgUrls.size(); i++) {
                File file = new File(imgUrls.get(i).trim());
                bodyMap.put(3 + "\"; filename=\"" + file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
            }
        }
        return bodyMap;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        imgUrls.addAll(data.getStringArrayListExtra("imgUrls"));
        adapter.setItems(imgUrls);
    }


    @Override
    public void onSuccess(String object) {
        AppPublicMethod.showToast(this, object);
        finish();
    }

    @Override
    public void onError(String object) {

    }

    @OnClick(R.id.tv_comment)
    public void onViewClicked() {
        getData();
    }

}
